//登陆改变图标
function changeIcon(){
    var token = selectByKey("like_token");
    if(token){
        var imgs = document.getElementsByTagName("a")[1];
        imgs.style.backgroundImage = "url('../../statics/images/index/Group.png')";
        imgs.style.backgroundSize = "23px 23px";
        imgs.style.backgroundPosition = "-1px 0px";
        imgs.style.backgroundRepeat = "no-repeat";
    }
}
function s(select) {
    return document.querySelector(select);
}
function shoucan() {
    var scs = document.querySelectorAll('.sc'), i = 0;
    for (; i < scs.length; i++) {
        Tap(scs[i], function (e, dom) {
            var pngname = dom.src.split('/')[dom.src.split('/').length - 1];
            if (pngname == 'wsc.png') {
                dom.src = '../../statics/images/school/sc.png';

            } else {
                dom.src = '../../statics/images/school/wsc.png';

            }
        })
    }
};
//收藏模块END
function jshtml(data, scriptdom) {
    var mainmax = document.getElementById('fmain');
    var html = '';
    for (var k in data) {
        var htmlm = scriptdom.innerHTML;
        htmlm = htmlm.replace(/\{\{imgsrc\}\}/, data[k].imgsrc);
        htmlm = htmlm.replace(/\{\{time\}\}/, data[k].time);
        htmlm = htmlm.replace(/\{\{text\}\}/, data[k].text);
        html += htmlm;
    }
    mainmax.innerHTML = html;
};
function getTimeNum (time) {
    var date = {}
    var preTime = time.replace(/\-/g,'/');
    var preTimeTurn = Date.parse(new Date(preTime));
    var nowDate = new Date().getTime();
    var timeCha = (nowDate - preTimeTurn)/1000;
    if(timeCha / 3600 < 1){
        date.num = parseInt(timeCha / 60);
        date.timeType = '分钟';
        return date;
    }else if(timeCha / 86400 < 1){
        date.num = parseInt(timeCha / 3600);
        date.timeType = '小时';
        return date;
    }else if(timeCha / 31104000 < 1){
        date.num = parseInt(timeCha / 86400);
        date.timeType = '天';
        return date;
    }else {
        date.num = parseInt(timeCha / 31104000);
        date.timeType = '年';
        return date;
    }
}
function toOtherDetail () {
    var videos = document.querySelectorAll('.video_list');
    for(var i = 0; i < videos.length; i++){
        Tap(videos[i],function(e,dom){
            var videoId = dom.getAttribute('data-video_id');
            window.location.href = 'videocontent.html?list=' + videoId;
        })
    }
}
function videoAjax(data,htmlMode,htmlNode) {
    var html = '';
    for(var i = 0; i < 2; i++){
        var htmlTemp = htmlMode.innerHTML;
        htmlTemp = htmlTemp.replace(/\{\{videoId\}\}/,data[i].id);
        htmlTemp = htmlTemp.replace(/\{\{imageSrc\}\}/,data[i].thumb_pic);
        htmlTemp = htmlTemp.replace(/\{\{videoName\}\}/,data[i].title);
        html += htmlTemp;
    }
    htmlNode.innerHTML = html;
}
//ajax模板END
window.onload = function () {
    var listId = location.search.split("=")[1];
    $.ajax({
        url: pathName + '/api/v1/wedding/detail',
        type: 'get',
        data:{
            'id': listId,
            'type': 2
        },
        success:function (res){
            document.querySelector('.title').innerHTML = res.wedding_article.title;
            document.querySelector('video').src = JSON.parse(res.wedding_article.content).mp4;
            document.querySelector('video').poster = res.wedding_article.thumb_pic;
            var timeObj = getTimeNum(res.wedding_article.updated_at);
            s('#timeNum').innerHTML = timeObj.num;
            s('#timeType').innerHTML = timeObj.timeType;
        }
    })
    changeIcon();
    var enter = document.querySelector("header a:last-child");
    Tap(enter,function(){
        if(selectByKey("like_token")){
            window.location.href = '../personal/individual.html';
        }else{
            window.location.href = '../personal/enter.html';
        }
    });
    $.ajax({
        url: pathName + '/api/v1/wedding',
        type: 'get',
        success: function (res) {
            console.log(res)
            videoAjax(res.video.data, s('#videoList'), s('.video_wrapper'))
            toOtherDetail ();
        }
    })


    shoucan();
};